home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
HyperCard
/
RTF XCMDs 1.3
/
RTFToField.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-29
|
4KB
|
223 lines
/*
* This software is copyright 1992 by Robert Morris.
* You may freely redistribute this software as shareware
* if you do so in the same form as you got it. If you find
* this software useful, please send $12 to:
* Robert Morris
* P.O. Box 1044
* Harvard Square Station
* Cambridge, MA 02238
* ecognome@aol.com
* If you incorporate any of this software in any kind of
* commercial product, please send $2 per copy distributed
* to the above address.
*/
/*
* Changes since 1.2:
* fixed a memory leak in CvtMyStyleHandle.
* set last styleIndex to -1, and last startChar to length+1.
* eliminated trailing null in text.
* allow field id as well as field name.
*/
/*
* RTFToField(rtf-text, cardflag, fieldname)
*/
#include <HyperXCmd.h>
#include <stdlib.h>
#include <ctype.h>
#include "SetUpA4.h"
#include <string.h>
#include "lists.h"
#include "rtf.h"
#define DUMP 0
XCmdPtr xptr;
void error(char *);
int goterror;
Handle HStr(char *);
void DumpField(TEHandle teh, struct list *out);
pascal void
main(paramPtr)
XCmdPtr paramPtr;
{
int i, fieldID, fieldNum;
TEHandle teh;
char fieldname[256];
Boolean cardFld;
TEStyleHandle sh1;
MyStyleHandle sh;
Handle txt, txt1;
struct list flds;
long start, nstart, txtlen;
OSErr err;
#if DUMP
struct list dump;
#endif
RememberA0();
SetUpA4();
xptr = paramPtr;
goterror = 0;
teh = 0;
sh = 0;
sh1 = 0;
txt = 0;
#if DUMP
dump.h = 0;
#endif
if(paramPtr->paramCount != 3){
error("error : usage RTFToField(rtf-text, cardflag, fieldname)\rCopyright 1992 Robert Morris.");
goto out;
}
parsertf(paramPtr->params[0], &sh, &txt);
if(goterror)
goto out;
if(strcmp(*(paramPtr->params[1]), "false") == 0)
cardFld = FALSE;
else
cardFld = TRUE;
start = 0;
txtlen = GetHandleSize(txt);
NewList(paramPtr->params[2], &flds);
while(ReadListLine(&flds, fieldname) != -1){
if(isdigit(fieldname[0])){
fieldID = atoi(fieldname);
fieldNum = 0;
} else {
CtoPstr(fieldname);
fieldID = fieldNum = 0;
}
teh = GetFieldTE(paramPtr, cardFld, fieldID, fieldNum,
fieldID ? 0 : (StringPtr)fieldname);
if(teh == 0 || paramPtr->result != xresSucc){
error("error : no such field");
goto out;
}
sh1 = GetStylHandle(teh);
if(sh1 == 0){
error("error : GetStylHandle() failed");
goto out;
}
nstart = start;
err = CvtMyStyleHandle(sh, &nstart, sh1, 30000L, txt);
if(err != 0){
error("error : out of memory");
goto out;
}
/* TESetText() destroys existing style information */
HLock(txt);
TESetText(*txt + start, nstart - start, teh);
HUnlock(txt);
/* so re-compute styles! */
nstart = start;
err = CvtMyStyleHandle(sh, &nstart, sh1, 30000L, txt);
#if DUMP
if(dump.h == 0)
DumpField(teh, &dump);
#endif
SetFieldTE(paramPtr, cardFld, fieldID, fieldNum,
fieldID ? 0 : (StringPtr)fieldname, teh);
TEDispose(teh);
teh = 0;
start = nstart;
}
out:
if(sh)
DisposMyStyleHandle(sh);
if(teh)
TEDispose(teh);
if(txt)
DisposHandle(txt);
if(goterror && paramPtr->returnValue == 0)
paramPtr->returnValue = HStr("error : out of memory!");
#if DUMP
if(goterror == 0)
paramPtr->returnValue = dump.h;
#endif
RestoreA4();
}
#if DUMP
void
DumpField(TEHandle teh, struct list *out)
{
int sprintf(char *, const char *, ...);
STHandle st;
TEStyleHandle sh;
Handle txt;
char tmp[256];
long i;
sh = GetStylHandle(teh);
if(sh == 0){
error("error : GetStylHandle() failed");
return;
}
st = (*sh)->styleTab;
txt = (Handle)TEGetText(teh);
NewList(0L, out);
sprintf(tmp, "teLength %d, real length %ld\r",
(*teh)->teLength, GetHandleSize(txt));
AppendList(out, tmp);
sprintf(tmp, "nRuns %d nStyles %d record len %ld\r",
(*sh)->nRuns, (*sh)->nStyles, GetHandleSize(sh));
AppendList(out, tmp);
for(i = 0; i <= (*sh)->nRuns; i++){
sprintf(tmp, " %ld: start %d index %d\r",
i, (*sh)->runs[i].startChar, (*sh)->runs[i].styleIndex);
AppendList(out, tmp);
}
sprintf(tmp, "styleTab len %ld\r",
GetHandleSize(st));
AppendList(out, tmp);
}
#endif
Handle
HStr(str)
char *str;
{
Handle newHndl;
newHndl = (Handle) NewHandle((long) strlen(str) + 1);
if(newHndl == 0)
return(0);
strcpy((char *) (*newHndl), str);
return(newHndl);
}
void
error(s)
char *s;
{
if(goterror == 0)
xptr->returnValue = HStr(s);
goterror = 1;
}